var ValidateUtils = {
	isNull : function(obj) {
		if (obj == null || obj === undefined || typeof (obj) == 'undefined') {
			return true;
		}
		return false;
	},

	getType : function(obj, type) {
		var typeVal = $(obj).attr(type);
		if (ValidateUtils.isNull(typeVal)) {
			return "";
		}
		return typeVal;
	},

	getDigitalFromBool : function(boolVal) {
		if (ValidateUtils.isNull(boolVal) || boolVal != true) {
			return 0;
		} else {
			return 1;
		}
	},

	getBoolFromDigital : function(digitalVal) {
		if (ValidateUtils.isNull(digitalVal) || digitalVal == 0) {
			return false;
		} else {
			return true;
		}
	},

	getValidateFunctions : function(typeVal) {
		var functions = [];
		if (typeVal.length < 1) {
			return functions;
		}
		if (typeVal.indexOf("null") != -1) {
			functions.push(ValidateUtils.NullValidate);
		}
		if (typeVal.indexOf("nullnumber") != -1) {
			functions.push(ValidateUtils.NullNumberValidate);
		}
		if (typeVal.indexOf("number") != -1) {
			functions.push(ValidateUtils.NumberValidate);
		}
		if (typeVal.indexOf("name") != -1) {
			functions.push(ValidateUtils.NameValidate);
		}
		if (typeVal.indexOf("email") != -1) {
			functions.push(ValidateUtils.EmailValidate);
		}

		if (typeVal.indexOf("zipcode") != -1) {
			functions.push(ValidateUtils.ZipcodeValidate);
		}
		if (typeVal.indexOf("range") != -1) {
			functions.push(ValidateUtils.RangeValidate);
		}
		if (typeVal.indexOf("compare") != -1) {
			functions.push(ValidateUtils.CompareValidate);
		}
		return functions;
	},

	NullValidate : function(obj, errorMessages) {
		obj = $(obj);
		if (obj.val().length < 1) {
			$(ValidateUtils.getType(obj, "error")).html(
					errorMessages[obj.attr("name") + "_Null"]).show();
			return false;
		}
		$(ValidateUtils.getType(obj, "error")).hide();
		return true;
	},

	NullNumberValidate : function(obj, errorMessages) {
		obj = $(obj);
		if (obj.val() == 0) {
			$(ValidateUtils.getType(obj, "error")).html(
					errorMessages[obj.attr("name") + "_Null"]).show();
			return false;
		}
		$(ValidateUtils.getType(obj, "error")).hide();
		return true;
	},

	NumberValidate : function(obj, errorMessages) {
		obj = $(obj);
		var numberPattern = /[0-9]+/;
		if (!numberPattern.test(obj.val())) {
			$(ValidateUtils.getType(obj, "error")).html(
					errorMessages[obj.attr("name") + "_Number"]).show();
			return false;
		}
		$(ValidateUtils.getType(obj, "error")).hide();
		return true;
	},

	NameValidate : function(obj, errorMessages) {
		obj = $(obj);
		var chracterPattern = /^[a-zA-Z\u3040-\u309F\u30A0-\u30FF\u4E00-\u9FA5\uFF21-\uFF3A\uFF66-\uFF9F\uFF21-\uFF3A\uFF41-\uFF5A\u0020-\u0020\u3000-\u3000]+$/;

		if (!chracterPattern.test(obj.val())) {
			$(ValidateUtils.getType(obj, "error")).html(
					errorMessages[obj.attr("name") + "_Name"]).show();
			return false;
		}
		$(ValidateUtils.getType(obj, "error")).hide();
		return true;
	},

	EmailValidate : function(obj, errorMessages) {
		obj = $(obj);
		var emailPattern = /^[A-Z_a-z0-9-\.]+@([A-Z_a-z0-9-]+\.)+[a-z0-9A-Z]{2,4}$/;

		if (!emailPattern.test(obj.val())) {
			$(ValidateUtils.getType(obj, "error")).html(
					errorMessages[obj.attr("name") + "_Email"]).show();
			return false;
		}
		$(ValidateUtils.getType(obj, "error")).hide();
		return true;
	},

	ZipcodeValidate : function(obj, errorMessages) {
		obj = $(obj);
		var cellPostalReg = /[0-9]{3}[-][0-9]{4}/;
		if (!cellPostalReg.test(obj.val()) || obj.val().length > 8) {
			$(ValidateUtils.getType(obj, "error")).html(
					errorMessages[obj.attr("name") + "_Zipcode"]).show();
			return false;
		}
		$(ValidateUtils.getType(obj, "error")).hide();
		return true;
	},

	RangeValidate : function(obj, errorMessages) {
		obj = $(obj);
		var min = ValidateUtils.getType(obj, "min");
		var max = ValidateUtils.getType(obj, "max");
		if (obj.val().length < min || obj.val().length > max) {
			$(ValidateUtils.getType(obj, "error")).html(
					errorMessages[obj.attr("name") + "_Range"]).show();
			return false;
		}
		$(ValidateUtils.getType(obj, "error")).hide();
		return true;
	},
	
	CompareValidate : function(obj, errorMessages){
		obj = $(obj);
		var compareObj = $( $(obj).attr("compare") );
		if( compareObj.length < 1 || obj.val() != compareObj.val() ){
			$(ValidateUtils.getType(obj, "error")).html(
					errorMessages[obj.attr("name") + "_Compare"]).show();
			return false;
		}
		$(ValidateUtils.getType(obj, "error")).hide();
		return true;		
	}

}

// checktype 分别为null/name/zipcode/number
// bindevent为需要绑定的事件

var buyValidate = function(formId, errorMessages, extraFunctions) {
	this.errorMessages = $.extend( {
		firstname_Null : "氏を入力してください。",
		firstname_Name : "氏はアルファベット、漢字、仮名で入力してください。",
		firstname_Range : "氏は25文字以内で入力してください。",
		lastname_Null : "名を入力してください。",
		lastname_Name : "名はアルファベット、漢字、仮名で入力してください。",
		lastname_Range : "名は25文字以内で入力してください。",

		zipcode_Null : "郵便番号を入力してください。",
		zipcode_Zipcode : "郵便番号を正しく入力してください。",

		prefectureId_Null : "都道府県を入力してください。",
		prefectureId_Zipcode : "都道府県を正しく入力してください。",

		address1_Null : "住所1を入力してください。",
		phone_Null : "電話番号を入力してください。"
	}, errorMessages);

	var needValidateObjs = $("#" + formId + " [validate='true']");
	var validates = [];
	var This = this;
	needValidateObjs
			.each(function() {
				var checktype = ValidateUtils.getType($(this), "checktype");
				var bindevent = ValidateUtils.getType($(this), "bindevent");
				var validateFucntions = ValidateUtils
						.getValidateFunctions(checktype);
				var self = this;
				var validate = function() {
					var result = true;
					if (checktype.length < 1) {
						return true;
					}

					for ( var index in validateFucntions) {
						if (ValidateUtils.isNull(index)
								|| ValidateUtils
										.isNull(validateFucntions[index])) {
							continue;
						}
						result = result
								&& validateFucntions[index](self,
										This.errorMessages);
					}
					if (!ValidateUtils.isNull(extraFunctions)) {
						var extraError = 1;
						if (jQuery.isFunction(extraFunctions)) {
							extraError = extraError
									& extraFunctions(self, result);
						} else {

							for ( var index in extraFunctions) {
								if (ValidateUtils.isNull(index)
										|| ValidateUtils
												.isNull(extraFunctions[index])) {
									continue;
								}
								extraError = extraError
										& extraFunctions[index](self, result);
							}
						}
						result = result && (!!extraError);
					}
					return result;
				}

				validates.push(validate);
				if (bindevent.indexOf(",") == -1) {
					$(this).bind(bindevent, validate);
					return;
				}

				var events = bindevent.split(",");
				for ( var index in evnets) {
					if (ValidateUtils.isNull(index)
							|| ValidateUtils.isNull(events[index])) {
						continue;
					}
					$(this).bind(events[index], validate);
				}

			});

	$("#" + formId).bind(
			"submit",
			function() {
				var succ = 1;
				for ( var index in validates) {
					if (ValidateUtils.isNull(index)
							|| ValidateUtils.isNull(validates[index])) {
						continue;
					}
					succ = succ
							& ValidateUtils.getDigitalFromBool(validates[index]
									());
				}
				return ValidateUtils.getBoolFromDigital(succ);
				//		return false;
			});
}